/*
 * @Author: fisher
 * @Date: 2024-02-18 21:58:20
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-02-26 10:41:50
 * @FilePath: \allback\controllers\indexController.js
 * @Description:
 */
const si = require('systeminformation')
exports.getSysInfo = async ctx => {
  try {
    const systemInfo = {}
    // 获取 GPU 信息
    const gpuinfo = await si.graphics()
    const nvdia = gpuinfo.controllers.find(item => item.vendor == 'NVIDIA')
    if (nvdia) {
      systemInfo.gpu = {
        name: nvdia.model,
        memoryTotal: nvdia.memoryTotal,
        memoryUsed: nvdia.memoryUsed,
        memoryFree: nvdia.memoryFree
      }
    } else {
      systemInfo.gpu = undefined
    }
    // 获取 CPU 信息
    const cpuinfo = await si.cpu()
    systemInfo.cpu = {
      name: cpuinfo.brand,
      cores: cpuinfo.cores
    }
    // 获取内存信息
    const memoryinfo = await si.mem()
    systemInfo.memory = {
      total: memoryinfo.total,
      used: memoryinfo.used,
      free: memoryinfo.free
    }
    // 获取操作系统信息
    const osinfo = await si.osInfo()
    systemInfo.os = {
      name: osinfo.platform,
      distro: osinfo.distro,
      arch: osinfo.arch
    }

    ctx.body = {
      status: true,
      message: '系统信息获取成功！',
      data: systemInfo
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

exports.getGpuInfo = async ctx => {
  try {
    // 获取 GPU 信息
    const gpuinfo = await si.graphics()
    let gpu = {}
    const nvdia = gpuinfo.controllers.find(item => item.vendor == 'NVIDIA')
    if (nvdia) {
      gpu = {
        name: nvdia.model,
        memoryTotal: nvdia.memoryTotal,
        memoryUsed: nvdia.memoryUsed,
        memoryFree: nvdia.memoryFree
      }
    } else {
      gpu = undefined
    }
    ctx.body = {
      status: true,
      message: 'gpu数据获取成功！',
      data: gpu
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
